//
//  SWRetweetView.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/24.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class SWRetweetView: UIView {

    var retweetViewConstraint:Constraint?
    
    var retWeetModel:SWHomeModel?{
        
        didSet{
            
            //解除 照片的 竖直方向的约束
            retweetViewConstraint?.uninstall()
            
            contentLabel.text = retWeetModel?.text
            
            //如果转发微博有照片
            if retWeetModel?.pic_urls?.count > 0{
                
                pictureView.hidden = false
                pictureView.dataArray = retWeetModel?.pic_urls
                
                pictureView.snp_makeConstraints(closure: { (make) -> Void in
                    
                   self.retweetViewConstraint = make.top.equalTo(contentLabel.snp_bottom).offset(ItemMargin).constraint
                })
                
            }else{
                //如果转发微博没照片
                pictureView.hidden = true
                
            }
            
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        
        //添加转发微博的内容
        addSubview(contentLabel)
        
        contentLabel.backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(10)
            make.top.equalTo(self)
            make.trailing.equalTo(self).offset(-10)

        }

        //添加转发微博的图片吧
        
        addSubview(pictureView)
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            
         self.retweetViewConstraint =  make.top.equalTo(contentLabel.snp_bottom).offset(10).constraint
        
            
            make.leading.equalTo(self).offset((ScreenWidth - (3 * ItemWH + 2 * ItemMargin))/2)
            
        }
    
        
    }
    
    
   // lazy var contentLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, preferredMaxLayoutWidth: ScreenWidth-20)
    
    lazy var contentLabel:UILabel = {
       
        let lab = UILabel(textColor: UIColor.grayColor(), fontSize: 15)
        
        lab.numberOfLines = 0
        return lab
    }()
    
    
    lazy var pictureView:SWPictureCollectionView = SWPictureCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

}
